package dataStructure.Queue;

import java.util.Scanner;

/**
 * @author lhn
 * @date 2024-04-10 11:52
 */

public class ArrayQueue {
    private int maxSize; // 数组最大容量
    private int front;  //队列头
    private int rear;   //队列尾
    private int[] arr;  //该数据的存入位置

    public ArrayQueue(int maxSize) {
        this.maxSize = maxSize;
        this.arr = new int[maxSize];
        front = -1; //指向队列头部，指向队列头的前一个位置
        rear = -1;  //指向队列尾部，指向队列尾的数据
    }
    public boolean isFull() {
        return rear == this.maxSize - 1;
    }
    public boolean isEmpty() {
        return this.front == this.rear;
    }
    public void addQueue(int n) {
        if (isFull()){
            System.out.println("队列已满不能加入数据");
            return;
        }
        rear++;
        this.arr[rear] = n;
    }
    public int getQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空,不能取数据");
        }
        front++;
        return this.arr[front];
    }
    public void showQueue() {
        if (isEmpty()){
            System.out.println("队列为空");
            return;
        }
        for (int i = this.front + 1; i < this.front + this.maxSize; i++) {
            System.out.print(this.arr[i] + " ");
        }
        System.out.println(); // 换行
    }
    public int headQueue() {
        if(isEmpty()){
            throw new RuntimeException("队列为空");
        } else {
            return this.arr[++front];
        }
    }
    public static void main(String[] args){
        //创建一个队列
        ArrayQueue arrayQueue = new ArrayQueue(3);
        char Key = ' ';//接口用户输入
        Scanner s= new Scanner(System.in);
        boolean loop = true;
        //输出菜单
        while(loop){
            System.out.println("s:显示队列");
            System.out.println("e:退出程序");
            System.out.println("a:添加数据到队列");
            System.out.println("g:从队列取出数据");
            System.out.println("h:查看队列头部数据");
            Key = s.next().charAt(0);//接收一个字符
            switch (Key){
                case  's':
                    arrayQueue.showQueue();
                    System.out.println("show funish");
                    break;
                case 'a':
                    System.out.println("请输入一个数");
                    int value= s.nextInt();
//                    System.out.println("value is " + value);
                    arrayQueue.addQueue(value);
                    break;
                case 'g':
                    try{
                        int res = arrayQueue.getQueue();
                        System.out.println("去除的数据为："+res);
                    }catch (Exception e){
                        System.out.println(e.getMessage());//捕捉异常信息
                    }
                    break;
                case 'h':
                    try{
                        int res = arrayQueue.headQueue();
                        System.out.println("去除的数据为："+res);
                    }catch (Exception e){
                        System.out.println(e.getMessage());//捕捉异常信息
                    }
                    break;
                case 'e':
                    s.close();
                    loop = false;
                    break;
                default:
                    break;
            }

        }
        System.out.println("程序退出");
    }

}
